﻿///

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace WP7NumberOnlyKeyBoard
{
    public partial class MainPage : PhoneApplicationPage
    {
		uint passCount = 0;
        readonly uint passLimit = 6;
        private string _password = "";
        private string _confirmPassword = "";
        readonly private string _passHeading = "Enter password Max of six digit";
        readonly private string _passConfirmHeading  = "confirm passowrd";
        private bool IsConfirmPassword = false;
        public MainPage()
        {
            InitializeComponent();
			passBox.PasswordChanged += new RoutedEventHandler(passBox_PasswordChanged);
        }
		        void passBox_PasswordChanged(object sender, RoutedEventArgs e)
        {
            if (passCount == passLimit && !IsConfirmPassword)
            {
                txtBlkPassBoxHeading.Text = _passConfirmHeading;
                _password = passBox.Password;
                passBox.Password = "";
                passCount = 0;
                IsConfirmPassword = true;
            }

            if (passCount >= passLimit && IsConfirmPassword)
            {
                //txtBlkPassBoxHeading.Text = "confirm passowrd";
                _confirmPassword = passBox.Password;
                passBox.Password = "";
                passCount = 0;
                bool b = VerifyPasswords();
                if (VerifyPasswords())
                {
                    MessageBox.Show("Pass");
                    IsConfirmPassword = false;
                    txtBlkPassBoxHeading.Text = _passHeading;
                }
                else
                {
                    MessageBox.Show("Password do not match");
                    IsConfirmPassword = false;
                    txtBlkPassBoxHeading.Text = _passHeading;
                }
            }
        }

        private bool VerifyPasswords()
        {
            //int compareResult = string.Compare(_password, _confirmPassword);
            if (_password != _confirmPassword)
                return false;
            else
                return true;
        }

		private void txtPassword_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
        	grdDialer.Visibility = Visibility.Visible;
        }

        private void txtPassword_LostFocus(object sender, System.Windows.RoutedEventArgs e)
        {
        	grdDialer.Visibility = Visibility.Collapsed;
        }

        //private void txtPassword_DoubleTap(object sender, System.Windows.Input.GestureEventArgs e)
        //{
        //    if (grdDialer.Visibility != Visibility.Visible)
        //        grdDialer.Visibility = Visibility.Visible;
        //    else
        //        grdDialer.Visibility = Visibility.Collapsed;
        //}

        private void btnDialer_1_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (passCount < passLimit)
            {
                passBox.Password = passBox.Password + 1;
                passCount++;
            }
        }

        private void btnDialer_2_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (passCount < passLimit)
            {
                passBox.Password = passBox.Password + 2;
                passCount++;
            }
        }

        private void btnDialer_3_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (passCount < passLimit)
            {
                passBox.Password = passBox.Password + 3;
                passCount++;
            }
        }

        private void btnDialer_4_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (passCount < passLimit)
            {
                passBox.Password = passBox.Password + 4;
                passCount++;
            }
        }

        private void btnDialer_5_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (passCount < passLimit)
            {
                passBox.Password = passBox.Password + 5;
                passCount++;
            }
        }

        private void btnDialer_6_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (passCount < passLimit)
            {
                passBox.Password = passBox.Password + 6;
                passCount++;
            }
        }

        private void btnDialer_7_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (passCount < passLimit)
            {
                passBox.Password = passBox.Password + 7;
                passCount++;
            }
        }

        private void btnDialer_8_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (passCount < passLimit)
            {
                passBox.Password = passBox.Password + 8;
                passCount++;
            }
        }

        private void btnDialer_9_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (passCount < passLimit)
            {
                passBox.Password = passBox.Password + 9;
                passCount++;
            }
        }

        private void btnDialer_ClearAll_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            passBox.Password = "";
            passCount = 0;
        }

        private void btnDialer_0_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (passCount < passLimit)
            {
                passBox.Password = passBox.Password + 0;
                passCount++;
            }
        }

        private void btnDialer_Back_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            int passlength = passBox.Password.Length;
            if (passlength == 0)
                return;
            passBox.Password = passBox.Password.Substring(0, passlength - 1);
            passCount--;
        }

        private void txtBox_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
        	//MessageBox.Show(e.ToString());
        }
    }
}
